from django.db import models
from datetime import datetime,timezone
# Create your models here.
# “话题”
class Topic(models.Model):
    topicName = models.CharField(max_length=100,default=f'New Topic')
    topicDescription = models.TextField(max_length=10000,default='An interesting topic...')
    addedTime = models.DateTimeField(auto_now_add=True)
    def __str__(self):
        ret= f'[{self.topicName}] {self.topicDescription}'
        if len(ret) > 50:
            ret=ret[:50]+'...'
        return ret[:53]
    def getTimeStr(self):
        rst=self.addedTime.strftime('%Y-%m-%d %H:%M:%S')
        return rst
    def getDescriptionShortly(self):
        rst=self.topicDescription
        if len(rst) > 40:
            rst=rst[:40]+'...'
        return rst
# “贴纸”
class Post(models.Model):
    topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
    postTitle = models.CharField(max_length=100,default=f'New Post')
    postContent = models.TextField(max_length=20000, default='Write something here...')
    addedTime = models.DateTimeField(auto_now=True)
    def __str__(self):
        ret= f'[{self.postTitle}](#{self.topic.topicName}) {self.postTitle}'
        if len(ret) > 50:
            ret=ret[:50]+f'...'
        return ret[:53]
    def getTimeStr(self):
        rst=self.addedTime.strftime('%Y-%m-%d %H:%M:%S')
        return rst